/*
 * GroupView.java
 *
 * Created on August 5, 2006, 9:29 PM
 */

package com.floreantpos.ui.views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import com.floreantpos.PosException;
import com.floreantpos.main.Application;
import com.floreantpos.model.Member;

import com.floreantpos.model.Ticket;
import com.floreantpos.model.TicketItem;

import com.floreantpos.swing.PosButton;
import com.floreantpos.ui.dialog.WeightedItemDialog;
import com.floreantpos.ui.views.order.SelectionView;
import com.floreantpos.ui.views.order.actions.ItemSelectionListener;
import java.awt.Color;
import java.util.ArrayList;

/**
 *
 * @author  MShahriar
 */
public class MemberSelectionView extends SelectionView {
    
	public final static String VIEW_NAME = "ITEM_VIEW";

	private Vector<ItemSelectionListener> listenerList = new Vector<ItemSelectionListener>();

        private List<MemberButton> memberButtonList;
	private List<Member> memberList;
        private Ticket currentTicket;
        private Member member;
      
	/** Creates new form GroupView */
    //pos constant
	public MemberSelectionView() {
		super("members");
                memberButtonList = new ArrayList<MemberButton>();
	}
        

        public Ticket getCurrentTicket() {
		return currentTicket;
	}

	public void setCurrentTicket(Ticket currentTicket) {
		this.currentTicket = currentTicket;
	}
	

	public void setMemberList(List<Member> memberList) {
		this.memberList = memberList;
		
		reset();
		
		if (memberList == null) {
			return;
		}

		
		try {
			

			for (int i = 0; i < memberList.size(); i++) {
				Member member = memberList.get(i);
				MemberButton itemButton = new MemberButton(member);
				addButton(itemButton);
                                memberButtonList.add(itemButton);
			}
                        this.clearMemberSelection();
			revalidate();
			repaint();
		} catch (PosException e) {
			e.printStackTrace();
		}
	}
        private void clearMemberSelection()
        {
            for (PosButton but:memberButtonList)
                but.setBackground(Color.lightGray);
        }
	public void addItemSelectionListener(ItemSelectionListener listener) {
		listenerList.add(listener);
	}

	public void removeItemSelectionListener(ItemSelectionListener listener) {
		listenerList.remove(listener);
	}

	private void fireItemSelected(Member member,MemberButton but) {
            this.clearMemberSelection();
            this.setMember(member);
            but.setBackground(Color.BLUE);
            
            //callBackListener.actionPerformed(null);
	}

	private void fireBackFromItemSelected() {
		
	}

	private class MemberButton extends PosButton implements ActionListener {
		Member member;
		
		MemberButton(Member member) {
			this.member = member;
			setText(member.getFname() + " " + member.getLname());
			addActionListener(this);
		}

		public void actionPerformed(ActionEvent e) {
			fireItemSelected(member,(MemberButton)e.getSource());
		}
	}

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }
        
	@Override
	public void doGoBack() {
		fireBackFromItemSelected();
	}
}
